from inspect import signature
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from datetime import timezone
from sqlalchemy import event  # 添加此行
from sqlalchemy.dialects.postgresql import UUID
import uuid  # 用于生成默认的 UUID 值

db = SQLAlchemy()


class User(db.Model):
    __tablename__ = 'users'  # 明确指定表名
    # id = db.Column(db.Integer, primary_key=True)
    id = db.Column(UUID(as_uuid=True), primary_key=True,
                   default=uuid.uuid4)  # 使用 UUID 类型

    phone = db.Column(db.String(11), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(128))

    avatar_url = db.Column(db.String(256), default='default_avatar.png')
    nickname = db.Column(db.String(64), default="网页用户")
    gender = db.Column(db.String(10), default="")
    signature = db.Column(db.Text, default="这个人很懒，什么都没有留下")
    long_term_memory = db.Column(db.Text, default='{}')

    created_at = db.Column(db.DateTime,
                           default=lambda: datetime.now(timezone.utc))


"""
class UserProfile(db.Model):
    __tablename__ = 'user_profiles'  # 明确指定表名

    user_id = db.Column(db.Integer,
                        db.ForeignKey('users.id'),
                        primary_key=True,
                        nullable=False)
    avatar_url = db.Column(db.String(256), default='default_avatar.png')
    nickname = db.Column(db.String(64))
    gender = db.Column(db.String(10))
    signature = db.Column(db.Text)


class Conversation(db.Model):
    __tablename__ = 'conversations'  # 新增表名定义
    id = db.Column(db.Integer, primary_key=True)
    # 修正外键引用（原 user.id → users.id）
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    title = db.Column(db.String(128))
    created_at = db.Column(db.DateTime,
                           default=lambda: datetime.now(timezone.utc))


class Message(db.Model):
    __tablename__ = 'messages'  # 新增表名定义
    id = db.Column(db.Integer, primary_key=True)
    # 修正外键引用（原 conversation.id → conversations.id）
    conversation_id = db.Column(db.Integer,
                                db.ForeignKey('conversations.id'),
                                nullable=False)
    role = db.Column(db.String(10), nullable=False)
    content = db.Column(db.Text)
    is_deleted = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime,
                           default=lambda: datetime.now(timezone.utc))
"""


class MoodRecord(db.Model):
    __tablename__ = 'mood_records'  # 新增表名定义
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)

    user_id = db.Column(UUID(as_uuid=True),
                        db.ForeignKey('users.id'),
                        nullable=False)

    category = db.Column(db.String(10), default="")  # 情绪类别
    emotion = db.Column(db.String(10), default="")  # 情绪
    content = db.Column(db.Text, default="")  # 记录内容
    date = db.Column(db.DateTime,
                     default=lambda: datetime.now(timezone.utc))  # 记录的创建日期
    summary = db.Column(db.Text, default="")  # 记录的总结
    image_url = db.Column(db.String(256), default="")  # 记录的图片 URL

    # image_urls = db.Column(db.ARRAY(db.String(256)))
    # created_at = db.Column(db.DateTime,
    #    default=lambda: datetime.now(timezone.utc))
